
import os
import django
import time
from django.core.cache import cache

# 这个必须在 django 建立环境之后
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.settings')
django.setup()


def basic_user():
    s = 'Hello World, Hello Django Cache'
    cache.set('key', s) # 存储缓存
    cache_result = cache.get('key') # 获取缓存
    print(cache_result)

    s2 = 'Hello World, Hello Django Timeout Cache'
    cache.set('key2', s2, 5) # 设置超时时间
    cache_result = cache.get('key2')  # 获取缓存
    print(cache_result)
    time.sleep(5) # 延迟5秒
    cache_result = cache.get('key2')  # 获取缓存
    print(cache_result)

if __name__ == '__main__':
    basic_user()